<template>
  <div class="login-container">
    <div class="login-box">
      <h2>{{ $t('loginTitle') }}</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">{{ $t('username') }}</label>
          <input 
            id="username" 
            v-model="loginForm.username" 
            type="text" 
            :placeholder="$t('usernamePlaceholder')" 
            required
          />
        </div>
        <div class="form-group">
          <label for="password">{{ $t('password') }}</label>
          <input 
            id="password" 
            v-model="loginForm.password" 
            type="password" 
            :placeholder="$t('passwordPlaceholder')" 
            required
          />
        </div>
        <button type="submit" class="login-button">{{ $t('login') }}</button>
      </form>
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'

// 响应式数据
const loginForm = ref({
  username: '',
  password: ''
})

const errorMessage = ref('')
const router = useRouter()

// 登录处理函数
const handleLogin = () => {
  // 简单的验证逻辑（实际项目中应该连接后端API）
  if (loginForm.value.username === 'admin' && loginForm.value.password === '123456') {
    // 保存登录状态到本地存储
    localStorage.setItem('isLoggedIn', 'true')
    // 跳转到首页
    router.push('/')
  } else {
    errorMessage.value = $t('loginError')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: var(--bg-color);
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  background: var(--header-bg);
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  border: 1px solid var(--border-color);
}

.login-box h2 {
  text-align: center;
  margin-bottom: 30px;
  color: var(--text-color);
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: var(--text-color);
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background-color: var(--bg-color);
  color: var(--text-color);
  font-size: 16px;
  box-sizing: border-box;
}

.form-group input:focus {
  outline: none;
  border-color: #42b983;
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-button:hover {
  background-color: #359c6d;
}

.error-message {
  margin-top: 20px;
  padding: 10px;
  background-color: #fee;
  color: #c33;
  border: 1px solid #fcc;
  border-radius: 4px;
  text-align: center;
}
</style>